﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problem18and67
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<long>> grid = ReadGrid("sample.txt");

            for (int i = grid.Count - 2; i >= 0; i--)
            {
                for (int j = 0; j < grid[i].Count; j++)
                {
                    grid[i][j] = grid[i][j] + (grid[i + 1][j] > grid[i + 1][j + 1] ? grid[i + 1][j] : grid[i + 1][j + 1]);
                }
            }

            Console.WriteLine(grid[0][0]);
            Console.ReadKey();
        }

        private static List<List<long>> ReadGrid(string fileName)
        {
            List<List<long>> grid = new List<List<long>>();

            foreach (string line in File.ReadAllLines(fileName))
            {
                if (String.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                List<long> numLine = new List<long>();
                string[] tokens = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string token in tokens)
                {
                    numLine.Add(long.Parse(token));
                }

                grid.Add(numLine);
            }

            return grid;
        }
    }
}
